define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/address/goods/index?qid='+Fast.api.query('qid'),
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");
            var brand_list = table.attr('brand-list');
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                queryParams: function (params) {
//                        var options = table.bootstrapTable('getOptions');
                    //这里可以追加搜索条件
//                        var filter = JSON.parse(params.filter);
//                        var op = JSON.parse(params.op);
//                        filter.referee_id=Fast.api.query('ref');
//                        params.filter = JSON.stringify(filter);
//                        params.op = JSON.stringify(op);
//                        return params;
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    filter.status = 1;
                    params.filter = JSON.stringify(filter);

                    return params;
                },
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'goods_title', title: __('Goods_title'),formatter: Controller.api.formatter.goods_info, operate: 'like',align: 'left'},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'cate_id', title: __('Cate_title'), visible: false, searchList: $.getJSON('goods/cateList')},
                        {field: 'brand_id', title: __('Brand_title'), visible: false, searchList: $.getJSON('goods/brandList')},
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'selling_price', title: __('Selling_price'), operate: false},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ]
            });


            $(table).on("click", "[data-id].btn-addgoods", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var qid = $(this).data("qid");
                $.post('box/address/goods/addgoods', {goods_id: id, qid: qid}, function (res) {
                    table.bootstrapTable('refresh', {});
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                goods_info: function (value, row, index) {
                    var html = row.goods_title + ',' + row.goods_model + ',' + row.goods_spec;
                    return html;
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a class="text-success btn-addgoods"  style="cursor:pointer" data-url="box/address/goods/addgoods" data-qid="' + Fast.api.query('qid') + '" data-id="' + row.goods_id + '"><b>添加</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});